package com.cheboohui.iqs.spider.pingan;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cheboohui.iqs.menu.ErrorNews;
import com.cheboohui.iqs.spider.login.LoginEntity;
import com.cheboohui.iqs.spider.pingan.entity.PAResult;
import com.cheboohui.iqs.spider.pingan.price.PriceStep;
import com.cheboohui.iqs.spring.entity.Price;
import com.cheboohui.iqs.spring.entity.price.request.PriceRequest;
import com.cheboohui.iqs.spring.entity.price.response.PriceResult;

/**
 * 平安爬虫车辆保费计算
 * 
 * @author xielonghai
 *
 */
public class CarPricePingAn {
	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(CarPricePingAn.class);

	/**
	 * 车险保费计算
	 * 
	 * @param priceRequest
	 * @return
	 */
	public static PriceResult getPrice(PriceRequest priceRequest, LoginEntity login) {
		if (!login.isState())
			return new PriceResult(ErrorNews.L02, "登录平安系统失败,未获取得到验证码");

		log.info(login.getOrderNo() + ",根据用户提供数据进行车险计算-->开始....");

		PriceStep step = new PriceStep(priceRequest, login);
		PAResult checkcar = step.getCheckCar();
		if (!checkcar.isState()) {
			log.info(login.getOrderNo() + ",根据用户提供数据进行车险计算-->车型查询或判断错误....");
			PriceResult res = new PriceResult(checkcar.getError(), checkcar.getErrorValue());
			if (ErrorNews.C05.equals(checkcar.getError()) || ErrorNews.B01.equals(checkcar.getError())) {
				if (null != checkcar.getCarList() && !checkcar.getCarList().isEmpty())
					res.setResult(new Price(CheckCarPingAn.toResult(checkcar.getCarList())));
			}
			return res;
		}

		PriceResult result = step.getPriceResult();
		log.info(login.getOrderNo() + ",根据用户提供数据进行车险计算-->结束....");
		return result;
	}
}
